package com.kfirszt.elearning.struts.actions;

import java.util.Arrays;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.encoding.BasePasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import com.kfirszt.elearning.base.BaseAction;
import com.kfirszt.elearning.dictionary.Ability;
import com.kfirszt.elearning.dictionary.CharacterTrait;
import com.kfirszt.elearning.dictionary.Interest;
import com.kfirszt.elearning.dictionary.Sex;
import com.kfirszt.elearning.persistence.model.City;
import com.kfirszt.elearning.persistence.model.Student;
import com.opensymphony.xwork2.ModelDriven;

@Result(name = "test", type = "redirect", location = "/test-input")
public class RegisterAction extends BaseAction implements ModelDriven<Student>{

	private static final long serialVersionUID = 1L;
	
	private Student student = new Student();
	
	@Autowired
    private AuthenticationManager authenticationManager;
	
	@Autowired
	private BasePasswordEncoder passwordEncoder;
	
	private String notEncodedPassword;
	
	@Override
	@Action(value = "register-input")
	public String input() throws Exception {
		return INPUT;
	}
	
	@Override
	public String execute() throws Exception {
		beforeSave();
		userService.createStudent(student);
		login();
		return "test";
	}

	private void beforeSave() {
		City city = getCityService().findByNameAndProvinceOrCreate(student.getCity().getName(), student.getCity().getProvince());
		student.setCity(city);
		notEncodedPassword = student.getPassword().toString();
		student.setPassword(passwordEncoder.encodePassword(student.getPassword(), null));
		
	}

	private void login() {
		UserDetails user = userService.loadUserByUsername(student.getUsername());
		UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), notEncodedPassword, user.getAuthorities());
		token.setDetails(new WebAuthenticationDetails(getServletRequest()));
	    Authentication authenticatedUser = authenticationManager.authenticate(token);
	    SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
	}

	@Override
	public Student getModel() {
		return student;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
	public List<Ability> getAbilitiesList(){
		return Arrays.asList(Ability.values());
	}
	
	public List<CharacterTrait> getCharacterTraitsList(){
		return Arrays.asList(CharacterTrait.values());
	}
	
	public List<Interest> getInterestsList(){
		return Arrays.asList(Interest.values());
	}
	
	public List<Sex> getSexsList(){
		return Arrays.asList(Sex.values());
	}
	
	
	
}
